package com.yiren.backstage.communication.services.impl;

import android.util.Log;

import com.yiren.backstage.communication.interfaces.DataCallback;
import com.yiren.backstage.communication.services.AbstractPortService;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;

public class SocketPortServiceImpl extends AbstractPortService {

    private String ip;

    private int port;

    private Socket socket;

    public SocketPortServiceImpl() {
        super();
    }

    @Override
    public void setParam(String key, Object obj) {
        if (key.equals("socket_ip")) {
            ip = (String) obj;
        } else if (key.equals("socket_port")) {
            port = (Integer) obj;
        }
    }

    @Override
    public boolean start() throws IOException {
        socket = new Socket();
        socket.connect(new InetSocketAddress(ip, port), 3000);
        //socket = new Socket(ip, port);
        inputStream = socket.getInputStream();
        outputStream = socket.getOutputStream();
        return true;
    }

    @Override
    public void stop() throws Exception {
        super.stop();
        if (socket != null) {
            socket.close();
            socket = null;
        }
    }

    @Override
    public String read(byte[] data, int offset, int size) throws IOException {
        return super.read(data, offset, size);
    }

    @Override
    public void write(byte[] data) throws IOException {
        super.write(data);
    }
}
